package com.jic.veteran.controller;

import com.jic.veteran.common.api.CommonPage;
import com.jic.veteran.common.api.CommonResult;
import com.jic.veteran.model.ProjectThanks;
import com.jic.veteran.service.ProjectThanksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "ProjectThanksController")
@Controller
@RequestMapping("/projectThanks")
public class ProjectThanksController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectThanksController.class);

    @Autowired
    private ProjectThanksService projectThanksService;

    @ApiOperation("根据项目编号获取感谢语列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<ProjectThanks>> list(@RequestParam(value = "projectId", required = false) String projectId,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<ProjectThanks> projectThanksList = projectThanksService.list(projectId, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(projectThanksList));
    }
    @ApiOperation("添加项目感谢语")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ProjectThanks projectThanks) {
        int count = projectThanksService.create(projectThanks);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改项目感谢语")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id,@RequestBody ProjectThanks projectThanks) {
        int count = projectThanksService.update(id,projectThanks);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取项目感谢语详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProjectThanks> getItem(@PathVariable Long id) {
        ProjectThanks projectThanks = projectThanksService.getItem(id);
        return CommonResult.success(projectThanks);
    }

    @ApiOperation("根据ID删除项目感谢语")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = projectThanksService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
